﻿using System.Text.Json;

namespace Qy.Core;

public class SerializeUtility
{
    /// <summary>
    /// 序列化
    /// </summary>
    /// <param name="item"></param>
    /// <returns></returns>
    public static byte[] Serialize(object item)
    {
        return JsonSerializer.SerializeToUtf8Bytes(item);
    }

    /// <summary>
    /// 反序列化
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="value"></param>
    /// <returns></returns>
    public static TEntity Deserialize<TEntity>(byte[] value)
    {
        if (value == null)
            return default;

        var utf8Reader = new Utf8JsonReader(value);
        return JsonSerializer.Deserialize<TEntity>(ref utf8Reader);
    }

}
